package com.sumainfor.zuul.service.impl.Feign;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.zuul.dao.Feign.MessageMapper;
import com.sumainfor.zuul.dao.UtilsMapper;
import com.sumainfor.zuul.entity.SysUserEntity;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能描述:
 * 各公司通知
 *
 * @Auther: zhlu
 * @Date: 2019/4/10 16:38
 * @Description:
 */
@Service
public class MessageService {

    private ReentrantLock lock = new ReentrantLock(false);

    @Autowired
    MessageMapper MessageMapper;

    @Autowired
    UtilsMapper utilsMapper;

    String UUID = null;

    /**
     * 功能描述:
     * 新增各部门消息通知
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/10 16:41
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean AddMessage(Map<String, Object> params) {
        boolean result = false;
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            try {
                //查询消息列表最大值
                Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                    put("uuid", "MeId");
                    put("database", Constants.DATA_SOURCE_SECURITY);
                    put("surface", "sys_message");
                }});
                if (uuid != null) {
                    int number = Integer.valueOf(uuid.get("uuid").toString());
                    UUID = ComUtils.getLocalTrmSeqNum("xx", number);
                } else {
                    UUID = ComUtils.getLocalTrmSeqNum("xx", 0);
                }

                //所有新增处理
                Map<String, Object> commonInsertParms = new HashMap<String, Object>();
                commonInsertParms.put("MeId", UUID);//编号
                commonInsertParms.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(commonInsertParms);
                params.putAll(commonInsertParms);
                //消息时间
                params.put("MessageTime", ConvertDateTime.getCurrentTime());

                //新增消息记录
                MessageMapper.AddMessage(params);
                result = true;
            } catch (NumberFormatException e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } finally {
            lock.unlock();
        }
        return result;
    }

    /**
     * 功能描述:
     * 根据角色获取消息列表
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/10 20:20
     */
    public List<Map<String, Object>> getMessageList(Map<String, Object> params) {
        List<Map<String, Object>> getMessageList = MessageMapper.getMessageList(params);
        return getMessageList;
    }

    /**
     * 功能描述:
     * 根据角色获取消息列表总数
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/10 20:20
     */
    public Integer getMessageListCount(Map<String, Object> params) {
        Integer getMessageListCount = MessageMapper.getMessageListCount(params);
        return getMessageListCount;
    }

    /**
     * 功能描述:
     * 根据编号修改为已读
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/25 16:34
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean UpdateMessageHaveRead(Map<String, Object> params) {
        boolean result = false;
        //所有修改处理
        Map<String, Object> commonUpdateParms = new HashMap<String, Object>();
        SysUserEntity userEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        commonUpdateParms.put("by", userEntity.getUserId());//操作人
//        commonUpdateParms.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
        commonUpdateParms.put("fnc",
                this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
        ParamsCommon.commonUpdateParms(commonUpdateParms);
        try {
            List<Map<String, Object>> multipleSelection = (List<Map<String, Object>>) params.get("multipleSelection");
            if (multipleSelection.size() > 0) {
                Map<String, Object> map;
                for (Map<String, Object> str : multipleSelection) {
                    map = new HashMap<String, Object>();
                    map.put("MeId", str.get("MeId"));//消息编号
                    map.put("IfRead", 1);//是否已读   0.未读   1.已读
                    map.putAll(commonUpdateParms);
                    //修改为已读
                    MessageMapper.UpdateMessageHaveRead(map);
                }
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

}
